/**
 * jims
 */
package com.jims.master.modules.operation.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.operation.entity.OperationDict;
import com.jims.master.modules.operation.entity.OperationMaster;
import com.jims.master.modules.operation.entity.OperationRoom;
import com.jims.master.modules.operation.service.OperationDictService;
import com.jims.master.modules.operation.service.OperationMasterService;
import com.jims.master.modules.operation.service.OperationRoomService;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.service.OrgDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 术后登记手术Controller
 * @author ZhuQ
 * @version 2017-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/operation/operationMaster")
public class OperationMasterController extends BaseController {

	@Autowired
	private OperationMasterService operationMasterService;
    @Autowired
    private OperationDictService operationDictService;
    @Autowired
    private OperationRoomService operationRoomService;
    @Autowired
    private OrgDeptService orgDeptService;
	
	@ModelAttribute
	public OperationMaster get(@RequestParam(required=false) String id) {
		OperationMaster entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = operationMasterService.get(id);
		}
		if (entity == null){
			entity = new OperationMaster();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(OperationMaster operationMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OperationMaster> page = operationMasterService.findPage(new Page<OperationMaster>(request, response), operationMaster); 
		model.addAttribute("page", page);
        model.addAttribute("operationMaster",operationMaster);
		return "modules/operation/operationMasterIndex";
	}

    /*
    * 术后登记：按照手术时间查询未登记/已登记手术列表
    * @ZhuQ
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findByOperationTime", ""})
    public String findByOperationTime(OperationMaster operationMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<OperationMaster> page = operationMasterService.findByOperationTime(new Page<OperationMaster>(request, response), operationMaster);
        model.addAttribute("page", page);
        model.addAttribute("operationMaster",operationMaster);
        return "modules/operation/operationMasterIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"findById", ""})
    public String findById(OperationMaster operationMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
        //获取手术详情
        operationMaster=operationMasterService.findById(operationMaster);
        model.addAttribute("operationMaster",operationMaster);

        OrgDept orgDept = new OrgDept();
        Page<OrgDept> orgDeptList = orgDeptService.findPage(new Page<OrgDept>(request, response), orgDept);
        model.addAttribute("orgDeptList", orgDeptList);
        OperationDict operationDict = new OperationDict();
        Page<OperationDict> operationDictList = operationDictService.findPage(new Page<OperationDict>(request, response), operationDict);
        model.addAttribute("operationDictList", operationDictList);
        OperationRoom operationRoom = new OperationRoom();
        Page<OperationRoom> operationRoomList = operationRoomService.findPage(new Page<OperationRoom>(request, response), operationRoom);
        model.addAttribute("operationRoomList", operationRoomList);

        return "modules/operation/operationMasterForm";
    }

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(OperationMaster operationMaster, Model model) {
		model.addAttribute("operationMaster", operationMaster);
		return "modules/operation/operationMasterForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(OperationMaster operationMaster, Model model) {
	    StringData data=new StringData();
		int i=operationMasterService.saveOperationMaster(operationMaster);
        if(i>0){
            data.setCode("success");
            data.setData("保存术后登记手术成功");
        }else{
            data.setCode("error");
            data.setData("保存术后登记手术失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(OperationMaster operationMaster) {
	    StringData data=new StringData();
		int i=operationMasterService.delete(operationMaster);
        if(i>0){
            data.setCode("success");
            data.setData("删除术后登记手术成功");
        }else{
            data.setCode("error");
            data.setData("删除术后登记手术失败");
        }
		return data;
	}

}